from django.http import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.template import RequestContext, Context
# from django.template import loader
from django.shortcuts import render

from django.contrib.auth import authenticate
from django.contrib.auth import login, logout

from models import LoginForm


def account_login(request):
    # Display login page
    if request.method == 'GET':
        if request.user and request.user.is_authenticated():
            return HttpResponseRedirect(reverse('home'))
        elif request.user and not request.user.is_authenticated():
            form = LoginForm()
            c = RequestContext(request, {'form': form})  # adds some headers needed for csrf
            # return render_to_response('account/login.html', {}, context_instance=c)
            return render(request, 'account/login.html', context_instance=c)
        # else:
        #     return HttpResponseServerError(loader.render_to_string('500.html'), {})
    # Login the user:
    elif request.method == 'POST':
        if request.user and not request.user.is_authenticated():
            try:
                username = request.POST['username']
                password = request.POST['password']

                user = authenticate(username=username, password=password)

                if user is not None:
                    if user.is_active:
                        login(request, user)
                        return HttpResponseRedirect(reverse('home'))
                    else:
                        form = LoginForm(request.POST)
                        c = RequestContext(request, {
                            'error_msg': 'Your account is not active anymore',
                            'form': form,
                            })
                        return render(request, 'account/login.html', context_instance=c)
                else:
                    form = LoginForm(request.POST)
                    c = RequestContext(request, {
                        'error_msg': 'Invalid username of password',
                        'form': form,
                        })
                    return render(request, 'account/login.html', context_instance=c)
            except:
                form = LoginForm()
                c = RequestContext(request, {
                    'error_msg': 'Invalid username or password or user does not exist',
                    'form': form,
                    })
                return render(request, 'account/login.html', context_instance=c)


def account_logout(request):
    if request.method == 'GET':
        if request.user and request.user.is_authenticated():
            return render(request, 'account/logout.html')
        elif request.user and not request.user.is_authenticated():
            c = Context({
                'error_msg': 'You are not logged in. Your cannot log out.',
                })
            return render(request, 'account/logout.html', context_instance=c)
    elif request.method == 'POST':
        logout(request)
        return HttpResponseRedirect(reverse('home'))
